package com.ztgf.shopping.api.cms.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgActualSearchBO;
import com.ztgf.shopping.models.bo.epg.EpgActualSearchSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgActualSearchDTO;
import com.ztgf.shopping.models.service.epg.IEpgActualSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * epg_actual_search控制器
 *
 * @author YUE
 * @Date 2020-09-22 17:10:54
 */

@RestController
@RequestMapping("/cms/epgActualSearch")
@Slf4j
@Api(value = "EpgActualSearchController",tags = "epg_actual_search接口")
public class EpgActualSearchController {

    @DubboReference
    private IEpgActualSearchService epgActualSearchService;

    @ApiOperation(value = "添加epg_actual_search",notes = "添加epg_actual_search")
    @PostMapping(value = "/insert")
    public Result insert(EpgActualSearchSaveBO saveBO) {
        boolean flag = epgActualSearchService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存epg_actual_search",notes = "保存epg_actual_search")
    public Result save(EpgActualSearchSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改epg_actual_search",notes = "修改epg_actual_search")
    @PostMapping("/update")
    public Result update(EpgActualSearchSaveBO saveBO){
        EpgActualSearchDTO dto = new EpgActualSearchDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = epgActualSearchService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除epg_actual_search",notes = "删除epg_actual_search")
    @ApiImplicitParam(name = "id",value = "epg_actual_searchid",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = epgActualSearchService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除epg_actual_search",notes = "批量删除epg_actual_search")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        epgActualSearchService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgActualSearchDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        EpgActualSearchDTO dto = epgActualSearchService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "epg_actual_search列表",notes = "epg_actual_search列表")
    @PostMapping("/queryPageList")
    public Result<Page<EpgActualSearchDTO>> queryPageList(EpgActualSearchBO bo){
        Wrapper<EpgActualSearchDTO> wrapper = new EntityWrapper<>();
        if (StrUtils.isNotEmpty(bo.getStartDate())){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (StrUtils.isNotEmpty(bo.getEndDate())){
            wrapper.le("create_time", bo.getEndDate());
        }
        wrapper.orderBy("search_num", false);
        Page<EpgActualSearchDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = epgActualSearchService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
